package riddles.content;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.os.Handler;
import android.provider.BaseColumns;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class RiddlesFavList extends Activity {
	private ListView listView = null;
	private ProgressDialog progressDialog = null;
	private RiddlesDbAdapter mDbHelper;
	private Cursor c;
	private ArrayList<Map<String, Object>> data = null;
	private Handler handler = new Handler();
	private Button out;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		long[] weight =getWeight();
		String choose = AdChoose.choose(weight);
		if (choose.equals("wiyun")) {
			setContentView(R.layout.favorites_joke_list_wiyun);
		} else if (choose.equals("wooboo")) {
			setContentView(R.layout.favorites_joke_list_wooboo);
		} else if (choose.equals("youmi")) {
			setContentView(R.layout.favorites_joke_list_youmi);
		} else {
			setContentView(R.layout.favorites_joke_list_admob);
		}
		listView = (ListView) findViewById(R.id.JokeList);
		out = (Button) findViewById(R.id.out);
		out.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(RiddlesFavList.this, Main.class);
				startActivity(i);
			}
		});
		out.setOnTouchListener(outListener);
		doSearch();
	}

	private void doSearch() {
		progressDialog = ProgressDialog.show(RiddlesFavList.this, "请稍等...",
				"获取查询内容...", true);
		new Thread() {
			public void run() {
				try {
					PrepareData();
					updateView();
				} catch (Exception e) {
					e.printStackTrace();
				}
				progressDialog.dismiss();
			}
		}.start();
	}

	private void PrepareData() {
		try {
			mDbHelper = new RiddlesDbAdapter(this);
			mDbHelper.open();
			c = mDbHelper.getAllNotes();
			startManagingCursor(c);
			data = new ArrayList<Map<String, Object>>();
			for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
				Map<String, Object> item = new HashMap<String, Object>();
				int id = c.getColumnIndex(BaseColumns._ID);
				int riddlesid = c.getColumnIndex(RiddlesDbAdapter.RIDDLESID);
				int question = c.getColumnIndex(RiddlesDbAdapter.QUESTION);
				int answer = c.getColumnIndex(RiddlesDbAdapter.ANSWER);
				item.put(BaseColumns._ID, "" + c.getLong(id));
				item.put(RiddlesDbAdapter.RIDDLESID, c.getString(riddlesid));
				item.put(RiddlesDbAdapter.QUESTION, c.getString(question));
				item.put(RiddlesDbAdapter.ANSWER, c.getString(answer));
				data.add(item);
			}
		} catch (SQLException e) {
			setTitle("删除菜谱失败");
		} finally {
			mDbHelper.closeclose();
		}

	}

	private void updateView() {
		handler.post(new Runnable() {
			public void run() {
				setView();
			}
		});
	}

	private void setView() {
		if (c.isAfterLast()) {
			SimpleAdapter adapter = new SimpleAdapter(this, data,
					R.layout.list_item, new String[] { "",
							RiddlesDbAdapter.QUESTION }, new int[] { R.id.name,
							R.id.content });
			listView.setAdapter(adapter);
			listView.setOnItemClickListener(listListener);
		} else {
			showToast(Toast.LENGTH_LONG, "无收藏!");
		}
	}

	protected void showToast(int type, String content) {

		View view = inflateView(R.layout.toast);

		TextView tv = (TextView) view.findViewById(R.id.content);
		tv.setText(content);

		Toast toast = new Toast(this);
		toast.setView(view);
		toast.setDuration(type);
		toast.show();
	}

	private View inflateView(int resource) {
		LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		return vi.inflate(resource, null);
	}

	private OnItemClickListener listListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Intent i = new Intent(RiddlesFavList.this, RiddlesFavView.class);
			Map<String, Object> map = data.get(position);
			Object o = map.get(BaseColumns._ID);
			i.putExtra(BaseColumns._ID, o.toString());
			startActivity(i);
		}
	};

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { // 按下的如果是BACK，同时没有重复
			Intent i = new Intent(RiddlesFavList.this, Main.class);
			startActivity(i);
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	OnTouchListener outListener = new OnTouchListener() {
		@Override
		public boolean onTouch(View v, MotionEvent event) {

			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				// 更改为按下时的背景图片
				out.setBackgroundResource(R.drawable.outover);
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				// 改为抬起时的图片
				out.setBackgroundResource(R.drawable.out);
			}
			return false;
		}

	};
	
	private long[] getWeight() {
		WeightDbAdapter mDbHelper = new WeightDbAdapter(this);
		long[] l = new long[4];
		try {
			Cursor c;
			mDbHelper.open();
			c = mDbHelper.getAllNotes();
			startManagingCursor(c);
			if (c.getCount() > 0) {
				c.moveToFirst();
				int admob = c.getColumnIndex(WeightDbAdapter.ADMOB);
				int youmi = c.getColumnIndex(WeightDbAdapter.YOUMI);
				int wooboo = c.getColumnIndex(WeightDbAdapter.WOOBOO);
				int wiyun = c.getColumnIndex(WeightDbAdapter.WIYUN);
				l[0] = new Long(c.getString(admob));
				l[1] = new Long(c.getString(youmi));
				l[2] = new Long(c.getString(wooboo));
				l[3] = new Long(c.getString(wiyun));
				c.close();
			} else {
				l[0] = 1;
				l[1] = 1;
				l[2] = 1;
				l[3] = 1;
			}
			return l;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			mDbHelper.closeclose();
		}
		return l;
	}
}
